<template>
  <el-container>
    <!-- 头部区 -->
    <el-header>
      <div>
        <span style="font-weight: bold; font-size: 20px">千寻后台管理系统</span>
      </div>
      <div>
        <el-button
          type="info"
          size="small"
          @click="logout"
          style="margin-left: 20px"
          >退出</el-button
        >
      </div>
    </el-header>
    <!-- 主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="200px">
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#4787d5"
          :default-active="active"
          @select="select"
          unique-opened
          router
        >
          <template v-for="(menuFistItem, menuFistIndex) in menulists">
            <el-submenu
              v-if="Array.isArray(menuFistItem.childern)"
              :key="menuFistIndex"
              :index="menuFistItem.path"
              :default-active="menuFistItem.path"
            >
              <template slot="title">
                <i :class="menuFistItem.icon"></i>
                <span>{{ menuFistItem.name }}</span>
              </template>
              <el-menu-item
                v-for="(
                  menuSecondItem, menuSecondIndex
                ) in menuFistItem.childern"
                :index="menuSecondItem.path"
                :key="menuSecondIndex"
              >
                <template slot="title">
                  <i :class="menuSecondItem.icon"></i>
                  <span>{{ menuSecondItem.name }}</span>
                </template>
              </el-menu-item>
            </el-submenu>

            <el-menu-item
              v-else
              :key="menuFistIndex"
              :index="menuFistItem.path"
            >
              <template slot="title">
                <i :class="menuFistItem.icon"></i>
                <span>{{ menuFistItem.name }}</span>
              </template>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 右侧内容 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { clearAccount } from '@/utils/login';
export default {
  data() {
    return {
      active: '',
    };
  },
  created() {
    this.active = window.sessionStorage.getItem('activeIndex') || 'repoList';
  },
  computed: {
    menulists() {
      let res = this.$router.options.routes.find(
        (item) => item.path === '/home'
      );
      console.log('menulists --> ', res.children);
      return res.children;
    },
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      // 伪SSO退出, 清除相关storage中的信息数据
      clearAccount();
      this.$router.push('/login');
    },
    select(index) {
      console.log('select --> ', index);
      window.sessionStorage.setItem('activeIndex', index);
      this.active = index;
    },
  },
};
</script>

<style lang="less" scoped>
/*el组件名，可以直接当类名用*/
.el-container {
  height: 100%;
}

.el-header {
  background-color: #373c41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  /*内部文本*/
  color: white;
  font-size: 20px;

  > div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #323744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #f6f8fa;
}
</style>
